package com.huluobo.a2308douyindemo

import android.content.Context
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.a2308douyindemo.adapter.CommonPagerAdapter
import com.huluobo.a2308douyindemo.databinding.ActivityMainBinding
import com.huluobo.a2308douyindemo.home.HomeFragment
import com.huluobo.a2308douyindemo.login.LoginViewModel
import com.huluobo.a2308douyindemo.message.MessageFragment
import com.huluobo.a2308douyindemo.mine.MineFragment
import com.huluobo.a2308douyindemo.mvvm.BaseActivity
import com.huluobo.a2308douyindemo.net.Const
import com.huluobo.a2308douyindemo.startLive.StartLiveActivity
import com.huluobo.a2308douyindemo.utils.MyToast
import com.huluobo.a2308douyindemo.videos.VideosFragment
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : BaseActivity<ActivityMainBinding, LoginViewModel>() {
    private val fragment = listOf(
        HomeFragment(),
        VideosFragment(),
        MessageFragment(),
        MineFragment()
    )

    override fun initData() {
        binding.mainVp2.adapter = CommonPagerAdapter(this, fragment)

//        Toast.makeText(this, "弹了个窗", Toast.LENGTH_LONG).show()
//        MyToast(this, "弹了个窗").show()
//        ToastUtils.showLong("弹了个窗")
//        ToastUtils.showLong(R.string.login)

//        //可封装的部分  1 2 4
//        //1.获取sp  由于sp不是new出来的,是通过context中的getSharedPreferences 拿到的,所以封装的时候需要有提供一个context
//        val sharedPreferences = this.getSharedPreferences("mySp", Context.MODE_PRIVATE)
//        //2.获取sp的编辑器
//        val edit = sharedPreferences.edit()
//        //3.提交内容
//        edit.putString(Const.PARAM_USERNAME, "lc22222")
//        //4.提交
//        edit.commit()
    }

    override fun initView() {
        //vp2和底部导航的联动
        binding.mainVp2.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                //处理页面的下标
                when (position) {
                    //由于底部导航类似一个布局,所以其中的item都是在menu中的,去处理逻辑也需要在menu的item中进行
                    0 -> binding.mainNav.menu.getItem(0).isChecked = true
                    1 -> binding.mainNav.menu.getItem(1).isChecked = true
                    2 -> binding.mainNav.menu.getItem(3).isChecked = true
                    3 -> binding.mainNav.menu.getItem(4).isChecked = true
                }
            }
        })

        //底部导航和vp2的联动
        binding.mainNav.setOnItemSelectedListener {
            when (it.itemId) {
                R.id.main_tab_home -> binding.mainVp2.currentItem = 0
                R.id.main_tab_video -> binding.mainVp2.currentItem = 1
                R.id.main_tab_start_live -> startAct(StartLiveActivity::class.java)
                R.id.main_tab_message -> binding.mainVp2.currentItem = 2
                R.id.main_tab_mine -> binding.mainVp2.currentItem = 3
            }
            true
        }
    }

    override fun onResume() {
        super.onResume()
        //只要切换到主页,就展示视频页面
        binding.mainVp2.currentItem = 1
        binding.mainNav.menu.getItem(1).isChecked = true
    }
}